package com.blackbrocast.controller;

import com.blackbrocast.mapper.BrocastMapper;
import com.blackbrocast.pojo.Brocast;
import com.blackbrocast.tool.Pagenatior;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(description = "黑广播")
@RequestMapping("/broadcast")
public class BroadcastController {

    @Autowired
    private BrocastMapper brocastMapper;

    @GetMapping("/getBroadcast")
    @ApiOperation("查询黑广播")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer", defaultValue = "10", paramType = "query"),
    })
    public HashMap<String, Object> getBroadcast(HttpServletRequest req) {
//        --- 接收 ---
        int limit = Integer.parseInt(req.getParameter("limit"));
        int page = Integer.parseInt(req.getParameter("page"));
        HashMap<String, Object> params = new HashMap<>();
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            params.put(name, req.getParameter(name));
        }
//        --- 处理 ---
        List<Brocast> brocasts = brocastMapper.getBrocast(params);
        Pagenatior pagenatior = new Pagenatior(brocasts, limit);
        List<Object> brocasts1 = pagenatior.page(page);
        HashMap<String, Object> con = new HashMap<>();
        con.put("data", brocasts1);
        con.put("count", pagenatior.getCount());
//        --- 返回 ---
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", "1");
        result.put("message", "success");
        result.put("data", con);
        return result;
    }
}
